package io.renren.wechat.support;

import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import io.renren.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @author dmcp@qq.com
 * @since 2021/12/10 22:49
 */
@RestControllerAdvice
public class ExceptionHandlingController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlingController.class);

    @ExceptionHandler({SQLException.class, DataAccessException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R handleDataAccessException(Exception exception) {
        LOGGER.error("handleDataAccessException, err: {}", exception.getMessage(), exception);

        return R.error("系统处理出错了");
    }

    @ExceptionHandler({DuplicateKeyException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R dataDuplicated(Exception exception) {
        LOGGER.error("dataDuplicated, err: {}", exception.getMessage(), exception);

        return R.error("数据重复了");
    }

    @ExceptionHandler({IllegalArgumentException.class})
    public R handleIllegalArgumentException(Exception exception) {
        LOGGER.error("handleIllegalArgumentException, err: {}", exception.getMessage(), exception);

        return R.error("输入有参数有误");
    }

    @ExceptionHandler({BindException.class})
    public R handleBindException(Exception exception) {
        LOGGER.error("handleBindException, err: {}", exception.getMessage(), exception);

        return R.error("输入的参数无效");
    }

    @ExceptionHandler({ApiException.class})
    public R handleApiException(Exception exception) {
        final String exceptionMessage = exception.getMessage();
        LOGGER.error("handleApiException, err: {}", exceptionMessage, exception);

        return R.error("后台接口执行出错");
    }

    @ExceptionHandler({HttpMessageNotReadableException.class})
    public R httpMessageNotReadableException(Exception exception) {
        LOGGER.error("handle httpMessageNotReadableException, err: {}", exception.getMessage(), exception);

        return R.error("无效请求或必要参数缺失");
    }

    @ExceptionHandler(value = UnsupportedOperationException.class)
    public R unsupportedOperation(Exception exception) {
        LOGGER.error(exception.getMessage(), exception);

        return R.error("不支持的操作");
    }

    @ExceptionHandler(value = RuntimeException.class)
    public R runtimeException(RuntimeException exception) {
        LOGGER.error("runtimeException-", exception);

        return R.error(exception.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R handleException(Exception exception) {
        LOGGER.error("!!!system exception happened!!!", exception);

        return R.error("系统内部错误");
    }
}
